import lib
import os
import time
import datetime
import sys
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import datastore_errors
from model.models import *
from lib import workflowDispatcher

class WorkflowManager():
  def requestCreated(self,request):
    logging.debug("request created")
   
    self.processRequest(request)
  
  def processRequest(self,request):
    workflow = request.workflow
    logging.debug("Processing request " + request.name)
    try:
      current_step = PSStep.get(request.pending_steps[0])
      self.processStep(current_step)
    except Exception, e:
      logging.error(str(e))
    
  def canProcessStep(self,step,user,company):
     logging.debug("Checkin if step can be modified")
     processor = workflowDispatcher.WorkflowDispatcher().getProcessorForStep(step)
     if processor is not None:
       return processor.canExecute(user,company)
     return False
  
  def processStep(self,step):
     
     logging.debug("ProcessStep")
     processor = workflowDispatcher.WorkflowDispatcher().getProcessorForStep(step)
     if processor is not None:
	processor.execute()
     
  
  
  def requestModified(self,request):
    logging.debug("Request modified")
    
    workflow = request.workflow
    
    current_step = PSStep.get(request.pending_steps[0])
    logging.debug(current_step)
    if current_step.status == "denied" :
      self.denyRequest(request)
    elif current_step.status == "approved":
      self.approveRequest(request)
    elif current_step.status == "cancelled":
      self.cancelRequest(request)
    elif current_step.status == "failed":
      self.failRequest(request)
      
  ##If a request is denied, all the previous involved members are notified of the denial
  def denyRequest(self,request):
    request.status = "denied"
    request.close_date = datetime.datetime.now()
    request.last_updated = datetime.datetime.now()
    request.put()
    pass
  
  #If it fails... what do I do?
  def failRequest(self,request):
    request.status = "failed"
    request.close_date = datetime.datetime.now()
    request.last_updated = datetime.datetime.now()
    request.put()
  #If a request is approved, the next involved will get notified
  def approveRequest(self,request):
    logging.debug("Approving request")
    step_key = request.pending_steps[0]
    step = PSStep.get(step_key)
    logging.debug("Pending step is " + step.request.name + "... appending to passed steps")
    
    request.passed_steps.append(step.key())
    logging.debug("Removing key from pending steps")
    request.pending_steps.remove(step_key)
    logging.debug("Done")
    if len(request.pending_steps) == 0:
      if step.status == "approved":
	request.status = "complete"
	logging.debug("Request is complete")
	request.close_date = datetime.datetime.now()
	request.last_updated = datetime.datetime.now()
    else:
      #There are no more pending steps, then the request is complete
      logging.debug("Processing next step")
      logging.debug(str(request.pending_steps))
      next_step = PSStep.get(request.pending_steps[0])
      self.processStep(next_step)
    request.put()
  
  
  ##If the requestor cancels the request, it only notifies to requestor confirming the cancelation and to current involved.
  def cancelRequest(self,request):
    request.status = "cancelled"
    request.last_updated = datetime.datetime.now()
    request.close_date = datetime.datetime.now()
    request.put()